package com.cc.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cc.models.Power;
import com.cc.models.Result;
import com.cc.models.Role;
import com.cc.models.User;
import com.cc.services.PowerServiceI;
import com.cc.services.RoleServiceI;
import com.cc.services.UserServiceI;
import com.cc.util.BeanUtil;
import com.cc.util.StringUtils;

@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Autowired
	private PowerServiceI powerService;
	
	@Autowired
	private RoleServiceI roleService;
	
	@Autowired
	private UserServiceI userService;
	
	/**
	 * 用户列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="userList",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Result userList(HttpServletRequest request,HttpServletResponse response){
		Result result = new Result();
		String pageSize = request.getParameter("limit");
		String offset = request.getParameter("offset");
		Map<String, Object> map = BeanUtil.request2Map(request, User.class);
		int size = 10;
		int num = 0;
		if(!StringUtils.isEmpty(pageSize)){
			size = Integer.valueOf(pageSize);
		}
		if(!StringUtils.isEmpty(offset)){
			num = Integer.valueOf(offset);
		}
		map.put("startRow",num);
		map.put("pageSize",size);
		List<User> list = userService.findUserWithPage(map);
		
		result.setStatus(Result.Status.OK.status);
		result.setMsg(Result.Message.OK.message);
		result.setRows(list);
		result.setTotal(userService.countByParams(map));
		return result;
	}
	/**
	 * 角色列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/roleList",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result roleList(HttpServletRequest request, HttpServletResponse response){
		Result result = new Result();
		String pageSize = request.getParameter("limit");
		String offset = request.getParameter("offset");
		Map<String, Object> map = BeanUtil.request2Map(request, Role.class);
		int size = 10;
		int num = 0;
		if(!StringUtils.isEmpty(pageSize)){
			size = Integer.valueOf(pageSize);
		}
		if(!StringUtils.isEmpty(offset)){
			num = Integer.valueOf(offset);
		}
		map.put("startRow",num);
		map.put("pageSize",size);
		List<Role> list = roleService.findByPage(map);
		
		result.setStatus(Result.Status.OK.status);
		result.setMsg(Result.Message.OK.message);
		result.setRows(list);
		result.setTotal(roleService.countByParams(map));
		return result;
	}
	
	/**
	 * 新增角色
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/saveRole",method=RequestMethod.POST)
	@ResponseBody
	public Result addRole(HttpServletRequest request,HttpServletResponse response){
		Result result = new Result();
		Role role = BeanUtil.request2Bean(request, Role.class);
		roleService.saveRole(role);
		result.setStatus(Result.Status.OK.status);
		result.setMsg(Result.Message.OK.message);
		return result;
	}
	
	/**
	 * 删除角色
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="delRole",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result delRole(HttpServletRequest request,HttpServletResponse response){
		Result result = new Result();
		Role role = BeanUtil.request2Bean(request, Role.class);
		roleService.delRole(role.getRoleId());
		result.setStatus(Result.Status.OK.status);
		result.setMsg(Result.Message.OK.message);
		return result;
	}

	/**
	 * 查找角色的权限
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="findRolePowers",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result findRolePowers(HttpServletRequest request,HttpServletResponse response){
		Result result = new Result();
		Role role = BeanUtil.request2Bean(request, Role.class);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleId", role.getRoleId());
		List<Power> powers = roleService.findPowers(params);
		result.setStatus(Result.Status.OK.status);
		result.setMsg(Result.Message.OK.message);
		result.setRows(powers);
		return result;
	}
	/**
	 * 权限列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/powerList",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result powerList(HttpServletRequest request,HttpServletResponse response){
		Result result = new Result();
		String pageSize = request.getParameter("limit");
		String offset = request.getParameter("offset");
		Map<String, Object> map = BeanUtil.request2Map(request, Power.class);
		int size = 10;
		int num = 0;
		if(!StringUtils.isEmpty(pageSize)){
			size = Integer.valueOf(pageSize);
		}
		if(!StringUtils.isEmpty(offset)){
			num = Integer.valueOf(offset);
		}
		map.put("startRow",num);
		map.put("pageSize",size);
		List<Power> list = powerService.findByPage(map);
		
		result.setStatus(Result.Status.OK.status);
		result.setMsg(Result.Message.OK.message);
		result.setRows(BeanUtil.list2Tree(list, 0, ""));
		result.setTotal(powerService.countByParams(map));
		return result;
	}
	
	/**
	 * 新增权限
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/savePower",method=RequestMethod.POST)
	@ResponseBody
	public Result addPower(HttpServletRequest request,HttpServletResponse response){
		Result result = new Result();
		Power power = BeanUtil.request2Bean(request, Power.class);
		powerService.savePower(power);
		result.setStatus(Result.Status.OK.status);
		result.setMsg(Result.Message.OK.message);
		return result;
	}
	
	/**
	 * 删除权限
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="delPower",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result delPower(HttpServletRequest request,HttpServletResponse response){
		Result result = new Result();
		Power power = BeanUtil.request2Bean(request, Power.class);
		powerService.delPower(power.getPowerId());
		result.setStatus(Result.Status.OK.status);
		result.setMsg(Result.Message.OK.message);
		return result;
	}
	
	// ---------------------------一下是跳转方法-----------------------------------------
	@RequestMapping(value="toPowerList",method=RequestMethod.GET)
	public String toPowerList(){
		return "sys/powerList";
	}
	@RequestMapping(value="toUserList",method=RequestMethod.GET)
	public String toUserList(){
		return "sys/userList";
	}
	@RequestMapping(value="toRoleList",method=RequestMethod.GET)
	public String toRoleList(){
		return "sys/roleList";
	}
}
